
package gameserver;

import common.Client;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Runs the accept loop.
 *
 * @author xissburg
 */
public class GameServer implements Runnable
{
    private Game game;
    private ServerSocket serverSocket;

    public GameServer()
    {
        game = new Game();
        serverSocket = null;
    }

    public void bind(int port) throws IOException
    {
        serverSocket = new ServerSocket(port);
    }

    /**
     * Runs the basic <i>accept loop</i>. For each new socket accepted a new
     * Client instance is created and a new ClientThread is started. The client
     * thread is responsible for performing the authentication process and if
     * successful it adds this new client to the client list and starts the
     * receive-handle-loop.
     */
    public void run()
    {
        while(true)
        {
            try
            {
                Socket socket = serverSocket.accept();
                Client client = new Client(socket);

                //TODO: read more about Executor and possibly change this
                new Thread(new ClientThread(client, game)).start();

                //System.out.println("New Client");
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
